package com.example.demo.Calss16_graph;

/**
 * @program: java_base
 * @description: 从给定数据结构，转换为自定义Graph结构接口实现 demo
 * @author: zhouhongtao
 * @happyCreateTime: 2022/03/02 09:56
 */
public class GraphGenerator {

    // matrix 所有的边
    // N*3 的矩阵
    // [weight, from节点上面的值，to节点上面的值]
    //
    // [ 5 , 0 , 7]
    // [ 3 , 0,  1]
    //
    public static Graph createGraph(int[][] matrix) {
        Graph graph = new Graph();
        for (int[] ints :matrix){
            int weight = ints[0];
            int from = ints[1];
            int to = ints[2];

            if (graph.nodes.containsKey(from)){
                graph.nodes.put(from, new Node(from));
            }
            if (graph.nodes.containsKey(to)){
                graph.nodes.put(to, new Node(to));
            }

            Node fromNode = graph.nodes.get(from);
            Node toNode =  graph.nodes.get(to);
            // 要不要重写 edge的hash() 算法那？
            Edge edge = new Edge(weight, fromNode, toNode);

            fromNode.edges.add(edge);
            fromNode.nexts.add(toNode);
            fromNode.out += 1;
            toNode.in += 1;

            graph.edges.add(edge);
        }
        return graph;
    }
}
